package com.srk.Jface.Helloworld;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class JfaceViewer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Display exampleDisplay = new Display();
		Shell exampleShell = new Shell(exampleDisplay);

		exampleShell.setBounds(120, 120, 220, 220);
		exampleShell.setLayout(new FillLayout());

		final TreeViewer myTreeViewer = new TreeViewer(exampleShell, SWT.SINGLE);

		myTreeViewer.setLabelProvider(new MyTreeLabelProvider());

		myTreeViewer.setContentProvider(new MyTreeContentProvider());

		myTreeViewer.setInput(Example1.getInput());

		exampleShell.open();

		while (!exampleShell.isDisposed()) {
			if (!exampleDisplay.readAndDispatch())
				exampleDisplay.sleep();
		}
		exampleDisplay.dispose();
	}

}

class MyTreeContentProvider extends ArrayContentProvider implements
		ITreeContentProvider {

	public Object[] getChildren(Object parent) {
		Example1 ex1 = (Example1) parent;
		return ex1.children;
	}

	public Object getParent(Object element) {
		Example1 ex1 = (Example1) element;
		return ex1.parent;
	}

	public boolean hasChildren(Object element) {
		Example1 ex1 = (Example1) element;
		return ex1.children.length > 0;
	}
}

class MyTreeLabelProvider extends LabelProvider {
	public Image getImage(Object element) {
		return null;
	}

	public String getText(Object element) {
		Example1 ex1 = (Example1) element;
		return ex1.name+"srk";
	}
}

class Example1 {
	String name;
	

	Example1[] children = new Example1[0];

	Example1 parent = null;

	Example1(String name) {
		this.name = name;
	}

	Example1(String name, Example1[] children) {
		this.name = name;
		this.children = children;
		for (int i = 0; i < children.length; i++) {
			children[i].parent = this;
		}
	}

	public static Example1[] getInput() {
		return new Example1[] {
				new Example1("First", new Example1[] { new Example1("Child1"),
						new Example1("Child2") }),
				new Example1("Second", new Example1[] { new Example1("Child3"),
						new Example1("Child4") }) };
	}
}
